package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.WaterMarkDTO;
import com.xbongbong.paas.pojo.vo.WaterMarkVO;
import com.xbongbong.paas.service.WaterMarkService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.help.SaasSaveHelp;
import com.xbongbong.saas.service.InventoryService;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @Description: 水印控制层
 * @Author: 肖云涛
 * @Date: 2022/8/29 15:34
 */

@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/waterMark")
public class WaterMarkController {


    @Resource
    private WaterMarkService waterMarkService;

    @RequestMapping(value = "/getWaterMark", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getWaterMark(@RequestBody @Valid WaterMarkDTO waterMarkDTO, BindingResult br) {
        XbbResponse<WaterMarkVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WaterMarkVO waterMarkVO = waterMarkService.getWaterMark(waterMarkDTO.getCorpid());
                response = new XbbResponse<>(waterMarkVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/updataWaterMark", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updataWaterMark(@RequestBody @Valid WaterMarkDTO waterMarkDTO, BindingResult br) {
        XbbResponse<WaterMarkVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WaterMarkVO waterMarkVO = waterMarkService.updataWaterMarkState(waterMarkDTO);
                response = new XbbResponse<>(waterMarkVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
